package com.xbongbong.util;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.Arith;
import com.xbongbong.pro.enums.errorcodes.ContractErrorCodeEnum;
import com.xbongbong.saas.enums.business.ContractOutstockEnum;

import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/08/04 10:24
 */
public class OutstockUtil {

    public static double getContractOutstockTotalMoney(JSONObject data, Long dataId, PaasFormDataEsListVO contractOutstockListVO) throws XbbException {
        double contractOutstockTotalMoney = 0D;
        // 新建时附初值，编辑跳过
        if (dataId == null) {
            contractOutstockTotalMoney = data.getDoubleValue(ContractOutstockEnum.TOTAL_MONEY.getAttr());
        }
        // 计算出库单总金额
        if (contractOutstockListVO == null) {
            throw new XbbException(ContractErrorCodeEnum.API_ERROR_203001.getCode(), ContractErrorCodeEnum.API_ERROR_203001.getMsg());
        }
        List<PaasFormDataEntityExt> contractOutstockList = contractOutstockListVO.getPaasFormDataESList();
        for (PaasFormDataEntityExt contractOutstock : contractOutstockList) {
            JSONObject contractOutstockData = contractOutstock.getData();
            Double contractOutstockMoney = contractOutstockData.getDouble(ContractOutstockEnum.TOTAL_MONEY.getAttr());
            if (Objects.nonNull(contractOutstockMoney)) {
                contractOutstockTotalMoney = Arith.add(contractOutstockTotalMoney, contractOutstockMoney);
            }
        }
        return contractOutstockTotalMoney;
    }
}
